package team.bluepen.supermarket.constant;

import team.bluepen.supermarket.util.StringUtil;

import javax.annotation.Nullable;

/**
 * 用户类型
 * @author Kai
 */
public enum Role {
    /** 管理员 */
    ADMIN,
    /** 普通用户 */
    USER,
    /** 游客 */
    GUEST;

    Role() {
    }

    @Override
    public String toString() {
        return name();
    }

    /**
     * 由字符串获取角色类别
     * @param role 字符串
     * @return Role
     */
    @Nullable
    public static Role get(String role) {
        if (StringUtil.isEmpty(role)){
            return null;
        }
        for (Role _r : values()){
            if (_r.name().equalsIgnoreCase(role)){
                return _r;
            }
        }
        throw new IllegalArgumentException("Cannot found Role by given text. 无法由给定字符串找到角色。");
    }
}
